home *** CD-ROM | disk | FTP | other *** search
- /* misc.c */
- /* written by : Jason R. Wilson */
-
- /* this package provides misc. routines such as normal computation */
-
- #include <math.h>
- #include <stdio.h>
- #include "datastruct.h"
-
-
- /***********************************************************************/
-
- void IdentifyVertices (ObjectCell *Object)
- {
-
- /* this routine goes through all the vertices and associates a unique
- integer with each one (it also counts them and stores the result
- in NoofVertices) */
-
- VertexCell *traverse;
- int count;
-
- count = 0;
- traverse = Object->VertexHead;
-
- while (!(traverse == NULL))
- {
- count++;
- traverse->Number = count;
- traverse = traverse->Next;
- }
-
- Object->NoofVertices = count;
- }
-
-
-
- /***********************************************************************/
-
- int IdentifyPolys (ObjectCell *ObjectHead)
-
- /* labels the polygons with a unique integer */
- /* returns the total number of polygons */
-
- {
- ObjectCell *Traverse;
- PolygonCell *CurrentPoly;
- int Total;
-
- Traverse = ObjectHead;
- Total = 0;
-
- while (!(Traverse == NULL))
- {
- CurrentPoly = Traverse->PolygonHead;
- while (!(CurrentPoly == NULL))
- {
- Total++;
- CurrentPoly->ID = Total;
- CurrentPoly = CurrentPoly->Next;
- }
- Traverse = Traverse->Next;
- }
-
- return Total;
- }
-
- /***********************************************************************/
-
-
- int IdentifyVerts (ObjectCell *ObjectHead)
-
- /* labels the vertices with a unique integer */
- /* returns the total number of vertices */
-
- {
- ObjectCell *Traverse;
- VertexCell *CurrentVert;
- int Total;
-
- Traverse = ObjectHead;
- Total = 0;
-
- while (!(Traverse == NULL))
- {
- CurrentVert = Traverse->VertexHead;
- while (!(CurrentVert == NULL))
- {
- Total++;
- CurrentVert->Number = Total;
- CurrentVert = CurrentVert->Next;
- }
- Traverse = Traverse->Next;
- }
-
- return Total;
- }
-
-
-
- /*----------------------------------------------------------------------*/
-
-
- void ComputeNormals (ObjectCell *Object)
-
- /* This routine computes the normals of the polygons and vertices of the
- object passed in */
- {
- PolygonCell *CurrentPoly;
- VertexCell *CurrentVert;
- VertexListCell *CurrentVertList;
- PolygonListCell *CurrentPolyList;
-
- int loop;
- int numpolys;
-
- Point pointi,pointj;
-
- /*------------------------------------------------------------*/
- /* First, compute polygon normals */
- CurrentPoly = Object->PolygonHead;
- while (!(CurrentPoly == NULL))
- {
- /*-------------------------------------------------------*/
- /* init. polygon normal */
- CurrentPoly->Normal.dx = 0.0;
- CurrentPoly->Normal.dy = 0.0;
- CurrentPoly->Normal.dz = 0.0;
- /*-------------------------------------------------------*/
-
- /* Compute Normal of Polygon using Newell's Method */
- CurrentVertList = CurrentPoly->Vertices;
- while (!(CurrentVertList == NULL))
- {
- pointi = CurrentVertList->Vertex->WorldPosition;
- if (CurrentVertList->Rest == NULL)
- pointj = CurrentPoly->Vertices->Vertex->WorldPosition;
- else
- pointj = CurrentVertList->Rest->Vertex->WorldPosition;
- CurrentPoly->Normal.dx += (pointi.y - pointj.y)*(pointi.z+pointj.z);
- CurrentPoly->Normal.dy += (pointi.z - pointj.z)*(pointi.x+pointj.x);
- CurrentPoly->Normal.dz += (pointi.x - pointj.x)*(pointi.y+pointj.y);
- CurrentVertList = CurrentVertList->Rest;
- }
-
- /*-------------------------------------------------------*/
-
- CurrentPoly = CurrentPoly->Next;
- }
-
- /*-------------------------------------------------------------*/
- /* Next, Compute Vertex Normals */
-
- CurrentVert = Object->VertexHead;
- while (!(CurrentVert == NULL))
- {
- /* init. vertex normal to zero (and numpolys) */
- CurrentVert->Normal.dx = 0.0;
- CurrentVert->Normal.dy = 0.0;
- CurrentVert->Normal.dz = 0.0;
- numpolys = 0;
-
- /*-------------------------------------------------------*/
- CurrentPolyList = CurrentVert->Polygons;
- while (!(CurrentPolyList == NULL))
- {
- numpolys++;
- CurrentVert->Normal.dx += CurrentPolyList->Polygon->Normal.dx;
- CurrentVert->Normal.dy += CurrentPolyList->Polygon->Normal.dy;
- CurrentVert->Normal.dz += CurrentPolyList->Polygon->Normal.dz;
- CurrentPolyList = CurrentPolyList->Rest;
- }
- CurrentVert->Normal.dx /= numpolys;
- CurrentVert->Normal.dy /= numpolys;
- CurrentVert->Normal.dz /= numpolys;
-
- CurrentVert = CurrentVert->Next;
- }
- /*-------------------------------------------------------------*/
-
- }
-
-
- /***********************************************************************/
-
- void CullBackFaces (ObjectCell *ObjectHead,double Eye,Point VRP,Vector n)
- /* marks the backfacing polygons culled */
- {
- Point WorldEye,q;
- PolygonCell *CurrentPoly;
- Vector nf;
- ObjectCell *Object;
-
- /* find eye in world coordinates */
-
- WorldEye.x = Eye*n.dx + VRP.x;
- WorldEye.y = Eye*n.dy + VRP.y;
- WorldEye.z = Eye*n.dz + VRP.z;
-
- /* go through polygons of all objects */
-
- Object = ObjectHead;
- while (!(Object == NULL))
- {
- CurrentPoly = Object->PolygonHead;
- while (!(CurrentPoly == NULL))
- {
- CurrentPoly->Culled = false; /* assume that it is not culled */
- nf = CurrentPoly->Normal;
- q = CurrentPoly->Vertices->Vertex->WorldPosition;
- /* just pick the first one */
-
- if ((nf.dx*(WorldEye.x - q.x) + nf.dy*(WorldEye.y - q.y) +
- nf.dz*(WorldEye.z - q.z)) <= 0.0)
- CurrentPoly->Culled = true;
- CurrentPoly = CurrentPoly->Next;
- }
- Object = Object->Next;
- }
- }
-
- /***********************************************************************/
-
-
-
-
-